<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {
	public function memberlist(){
		//取用户数据
		$user = M('user')->where(array('delete_state'=>'0'))->select();
		$this->assign('user',$user);

		$this->display();
	}

	public function state(){
		// 接post.id
		$id =  I('post.id');
		//连接数据库,查询用户状态
		$user = M('user')->where(array('id'=>$id))->find();

		if ($user['state']==0) {
			$data['id']=$id;
			$data['state']=1;
			M('user')->save($data);
			$arr = array('error'=>1,'info'=>'成功停用');
			echo json_encode($arr);
		}else{
			$data['id']=$id;
			$data['state']=0;
			M('user')->save($data);
			$arr = array('error'=>2,'info'=>'成功启用');
			echo json_encode($arr);
		}
		
	}

	public function membershow(){
		$id = I('get.id');//取页面get id
		$data = M('user')->where(array('id'=>$id))->find();//到数据库取用户数据
		// var_dump($data);
		$this->assign('u',$data);
		$this->display();
	}

	public function memberedit(){
		$id = I('get.id');
		$data = M('user')->where(array('id'=>$id))->find();//到数据库取用户数据
		$this->assign('u',$data);
		// var_dump($data);
		$this->display();
	}

	public function modifyuser() {
		$data = I("post.");
		$id = $data['id'];
		$res = M('user')->save($data);
		// var_dump($res);
		if ($res) {
			$arr = array('error'=>1,'info'=>'修改成功');
			echo json_encode($arr);
		}else{
			$arr = array('error'=>0,'info'=>'修改失败');
			echo json_encode($arr);
		}
	}

	public function memberpassword(){
		$id = I('get.id');
		$u = M('user')->where(array('id'=>$id))->find();
		$name = $u['username'];
		$this->assign('name',$name);
		$this->display();
	}

	public function memberpass()
	{
		$oldpass = I("post.oldpass");
		$newpass = I("post.newpass");
		$repass = I("post.repass");
		$id = I('post.id');
		// var_dump($id);
		$user = M('user')->where(array('id'=>$id))->find();
		if ($user['pass']!=md5($oldpass)) {
			$arr = array('error'=>0,'info'=>'原密码错误，请重新填写');
			echo json_encode($arr);
			exit();
		}else{
			if ($newpass!=$repass) {
				$arr = array('error'=>0,'info'=>'两次密码不一致');
				echo json_encode($arr);
				exit();
			}else{
				M('user')->save(array('id'=>$id,'pass'=>$newpass));
				$arr = array('error'=>1,'info'=>'修改成功');
				echo json_encode($arr);
			}
		}
	}

	public function deleteu() {
		$data['id'] = I('post.id');
		$data['delete_state'] = 1;
		$duser = M('user')->save($data);
		if ($duser) {
			$arr = array('error'=>1,'info'=>'删除成功');
			echo json_encode($arr);
		}else{
			$arr = array('error'=>0,'info'=>'删除失败');
			echo json_encode($arr);
		}
	}

	public function memberadd(){
		$this->display();
	}

	public function useradd(){
		//检查用户邮箱是否已被注册
		$res = D('user')->checkEmail(I('post.email'));
		if ($res!=null) {
			$arr = array(
    			'error'=>0,
    			'info'=>"该邮箱已被注册"
    			);
    		echo json_encode($arr);
    		exit;
		}
		//检查用户昵称是否被使用
		$res = D('user')->checkName(I('post.username'));
		if ($res!=null) {
    		$arr = array(
    			'error'=>0,
    			'info'=>"该昵称已被注册"
    			);
    		echo json_encode($arr);
    		exit;
    	}
    	//添加用户数据
    	$data = I('post.');
    	$data['pass'] = md5(I('post.pass'));
		$res = M('user')->add($data);

		if ($res) {
			$arr = array(
    			'error'=>1,
    			'info'=>"添加成功"
    			);
    		echo json_encode($arr);
		}else{
			$arr = array(
    			'error'=>0,
    			'info'=>"添加失败"
    			);
    		echo json_encode($arr);
		}
	}

	public function memberdel(){
		//取出删除状态的用户
		$user = M('user')->where(array('delete_state'=>'1'))->select();
		$this->assign('u',$user);
		$this->display();
	}

	public function userrecovery(){
		$id = I("post.");
		$data = array('delete_state'=>'0');
		$res = M("user")->where($id)->save($data);//修改删除状态

		if ($res) {
			$arr = array('error'=>1,'info'=>'成功恢复');
			echo json_encode($arr);
		}else{
			$arr = array('error'=>0,'info'=>'恢复失败');
			echo json_encode($arr);
		}
	}
}